package com.supersit.greensproject.Receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.supersit.greensproject.AsyncTask.UploadDataBatchUpload;
import com.supersit.greensproject.DB.XUtilsDbManager;
import com.supersit.greensproject.entity.DataUpload;
import com.supersit.greensproject.utils.L;
import com.supersit.greensproject.utils.SPUtils;
import com.supersit.greensproject.utils.T;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

import java.util.List;

/**
 * Created by Administrator on 2016/8/10 0010.
 */
public class ConnectionChangeReceiver extends BroadcastReceiver {
    private ConnectivityManager mConnectivityManager;
    private NetworkInfo netInfo;
    private int userId;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        userId = (int) SPUtils.get(context, "userId", -1);
        if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            netInfo = mConnectivityManager.getActiveNetworkInfo();
            if (netInfo != null) {
                T.showShort(context, "网络已连接");
                L.d("网络已连接");
                DbManager db = XUtilsDbManager.getInstance();
                List<DataUpload> list = null;
                try {
                    list = db.selector(DataUpload.class).where("isUpload", "=", 1).and("userId", "=", userId).findAll();
                } catch (DbException e) {
                    e.printStackTrace();
                }
                if (null != list && list.size() > 0) {
                    //进行批量上传
                    UploadDataBatchUpload uploadDataBatchUpload = new UploadDataBatchUpload(context);

                }
            } else {
                T.showShort(context, "网络断开");
                L.d("网络断开");
            }
        }
    }
}
